<template>
	<div class="personal layout-pd">
		<el-row>
			<!-- 个人信息 -->
			<el-col :xs="24" :sm="24">
				<el-card shadow="hover" header="个人信息">
					<div class="personal-user">
						<div class="personal-user-left">
							<el-image class="h100 personal-user-left-upload" :src="imgSrc" :preview-src-list="srcList" />
						</div>
						<div class="personal-user-right">
							<el-row>
								<el-col :span="24" class="personal-title mb25">{{ currentTime }},尊敬的用户,欢迎访问硬件实验成绩评定系统! </el-col>
								<el-col :span="24">
									<el-row>
										<el-col :xs="24" :sm="16" class="personal-item mb6">
											<div class="personal-item-label">{{ state.idLabel }}:</div>
											<div class="personal-item-value">{{ state.id }}</div>
										</el-col>
										<el-col :xs="24" :sm="8" class="personal-item mb6">
											<div class="personal-item-label">用户名:</div>
											<div class="personal-item-value">{{ state.userName }}</div>
										</el-col>
									</el-row>
								</el-col>
								<el-col :span="24">
									<el-row>
										<el-col :xs="24" :sm="16" class="personal-item mb6">
											<div class="personal-item-label">联系方式:</div>
											<div class="personal-item-value">{{ decrypt(userInfos.phone) }}</div>
										</el-col>
										<el-col :xs="24" :sm="8" class="personal-item mb6" v-show="state.ifStudent">
											<div class="personal-item-label">班级:</div>
											<div class="personal-item-value">{{ lesson_name }}</div>
										</el-col>
									</el-row>
								</el-col>
							</el-row>
						</div>
					</div>
				</el-card>
			</el-col>

			<!-- 项目风格 -->
			<el-col :span="24" v-show="state.idLabel == '账号'">
				<el-card shadow="hover" class="mt15" header="项目风格" style="height: 400px">
					<el-carousel :interval="3000" type="card" height="300px">
						<el-carousel-item v-for="item in carouselData" :key="item">
							<el-image class="carousel_image" :src="item.url" />
						</el-carousel-item>
					</el-carousel>
				</el-card>
			</el-col>

			<!-- 待办事项 -->
			<el-col :span="14" v-show="state.idLabel != '账号'">
				<el-card shadow="hover" class="mt15" header="实验提醒" style="height: 400px">
					<div v-show="state.experimentsInfoList.length == 0">
						<el-empty description="暂无数据"></el-empty>
					</div>
					<div class="personal-info-box">
						<ul class="personal-info-ul">
							<li v-for="(v, k) in state.experimentsInfoList" :key="k" class="personal-info-li">
								<a target="_blank" class="personal-info-li-title">{{ v.start_time }}您参与的在{{ v.classroom }}的实验{{ v.title }}{{ v.text }}</a>
								<el-divider />
							</li>
						</ul>
					</div>
				</el-card>
			</el-col>
			<el-col :span="10" v-show="state.idLabel != '账号'" style="flex: float">
				<el-card shadow="hover" class="mt15" header="评分提醒" style="height: 400px">
					<div v-show="state.gradeInfoList.length == 0">
						<el-empty description="暂无数据"></el-empty>
					</div>
					<div class="personal-info-box">
						<ul class="personal-info-ul">
							<li v-for="(v, k) in state.gradeInfoList" :key="k" class="personal-info-li">
								<router-link :to="{ path: '/grade/management' }" class="personal-info-li-title">您参与的实验{{ v.title }}有学生尚未评分</router-link>
								<el-divider />
							</li>
						</ul>
					</div>
				</el-card>
			</el-col>
		</el-row>
	</div>
</template>

<script setup lang="ts" name="home">
import { reactive, computed, ref } from 'vue';
import { formatAxis } from '/@/utils/formatTime';
import { Session } from '/@/utils/storage';
import code from '/@/assets/home/code.png';
import keyboard from '/@/assets/home/keyboard.png';
import paper from '/@/assets/home/paper.png';
import study from '/@/assets/home/study.png';
import operate from '/@/assets/home/operate.png';
import { storeToRefs } from 'pinia';
import { useUserInfo } from '/@/stores/userInfo';
import { decrypt } from '/@/utils/RSA';
import { date } from '/@/utils/date';
import { lessonInfo, teacherExperimentInfo, studentExperimentInfo, teacherGradeInfo } from '/@/api/home';
import { ElMessage } from 'element-plus';

const stores = useUserInfo();
const { userInfos } = storeToRefs(stores);

// 定义变量内容
const carouselData = [{ url: code }, { url: keyboard }, { url: paper }, { url: study }, { url: operate }];
const lesson_name = ref('');
const imgSrc =
	userInfos.value.avatar === ''
		? 'https://img2.baidu.com/it/u=1978192862,2048448374&fm=253&fmt=auto&app=138&f=JPEG?w=504&h=500'
		: userInfos.value.avatar;
const srcList =
	userInfos.value.avatar === ''
		? ['https://img2.baidu.com/it/u=1978192862,2048448374&fm=253&fmt=auto&app=138&f=JPEG?w=504&h=500']
		: [userInfos.value.avatar];
const state = reactive({
	id: '',
	idLabel: '',
	userName: '',
	ifStudent: false,
	experimentsInfoList: [],
	gradeInfoList: [],
});

const getRole = async () => {
	const userInfo = Session.get('userInfo');
	const nowTime = new Date();
	state.id = userInfo.Id;
	state.userName = userInfo.userName;
	if (userInfo.roles[0] == 'teacher') {
		state.idLabel = '教工号';
		teacherExperimentInfo(state.id).then((res) => {
			if (res.code === '2000') {
				state.experimentsInfoList = res.data.map((item: any) => {
					return {
						title: item.experiment_name,
						start_time: date(item.start_time),
						end_time: date(item.end_time),
						classroom: item.classroom,
						text: nowTime <= new Date(item.start_time) ? '未开始' : nowTime >= new Date(item.end_time) ? '已结束' : '进行中',
					};
				});
			} else {
				ElMessage.error('获取用户实验信息失败!');
			}
		});
		teacherGradeInfo(state.id).then((res) => {
			if (res.code === '2000') {
				state.gradeInfoList = res.data.map((item: any) => {
					return {
						title: item.experiment_name,
					};
				});
				state.gradeInfoList = Array.from(new Set(state.gradeInfoList.map(obj => JSON.stringify(obj)))).map(str => JSON.parse(str));
			} else {
				ElMessage.error('获取用户成绩信息失败!');
			}
		});
	} else if (userInfo.roles[0] == 'student') {
		state.idLabel = '学号';
		state.ifStudent = true;
		lessonInfo(userInfo.lesson_id).then((res) => {
			if (res.code === '2000') {
				lesson_name.value = res.data[0].lesson_name;
			} else {
				ElMessage.error('获取用户班级信息失败');
			}
		});
		studentExperimentInfo(userInfo.lesson_id)
			.then((res) => {
				if (res.code === '2000') {
					state.experimentsInfoList = res.data.map((item: any) => {
						return {
							title: item.experiment_name,
							start_time: date(item.start_time),
							end_time: date(item.end_time),
							classroom: item.classroom,
							text: nowTime <= new Date(item.start_time) ? '未开始' : nowTime >= new Date(item.end_time) ? '已结束' : '进行中',
						};
					});
				} else {
					ElMessage.error('获取用户实验信息失败!');
				}
			})
			.catch((err) => {
				ElMessage.error('获取用户实验信息失败!');
			});
	} else {
		state.idLabel = '账号';
	}
};

// 当前时间提示语
const currentTime = computed(() => {
	getRole();
	return formatAxis(new Date());
});
</script>

<style scoped lang="scss">
@import '../../theme/mixins/index.scss';
.personal {
	.personal-user {
		height: 130px;
		display: flex;
		align-items: center;
		.personal-user-left {
			width: 130px;
			height: 130px;
			border-radius: 3px;
			:deep(.el-upload) {
				height: 100%;
			}
			.personal-user-left-upload {
				img {
					width: 130%;
					height: 100%;
					border-radius: 3px;
				}
				&:hover {
					img {
						animation: logoAnimation 0.3s ease-in-out;
					}
				}
			}
		}
		.personal-user-right {
			flex: 1;
			padding: 0 60px;
			.personal-title {
				font-size: 18px;
				@include text-ellipsis(1);
			}
			.personal-item {
				display: flex;
				align-items: center;
				font-size: 15px;
				.personal-item-label {
					color: var(--el-text-color-secondary);
					@include text-ellipsis(1);
				}
				.personal-item-value {
					@include text-ellipsis(1);
				}
			}
		}
	}
	.personal-edit {
		.personal-edit-title {
			position: relative;
			padding-left: 10px;
			color: var(--el-text-color-regular);
			&::after {
				content: '';
				width: 2px;
				height: 10px;
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				background: var(--el-color-primary);
			}
		}
		.personal-edit-safe-box {
			border-bottom: 1px solid var(--el-border-color-light, #ebeef5);
			padding: 15px 0;
			.personal-edit-safe-item {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.personal-edit-safe-item-left {
					flex: 1;
					overflow: hidden;
					.personal-edit-safe-item-left-label {
						color: var(--el-text-color-regular);
						margin-bottom: 5px;
					}
					.personal-edit-safe-item-left-value {
						color: var(--el-text-color-secondary);
						@include text-ellipsis(1);
						margin-right: 15px;
					}
				}
			}
			&:last-of-type {
				padding-bottom: 0;
				border-bottom: none;
			}
		}
	}
}

.carousel_image {
	object-fit: scale-down;
	width: 100%;
	height: 100%;
}

.personal-info-box {
	height: 300px;
	overflow: auto;
	.personal-info-ul {
		list-style: none;
		.personal-info-li {
			font-size: 13px;
			.personal-info-li-title {
				display: inline-block;
				@include text-ellipsis(1);
				color: var(--el-text-color-secondary);
				text-decoration: none;
			}
			& a:hover {
				color: var(--el-color-primary);
				cursor: pointer;
			}
		}
	}
}
</style>
